# makefile for compiling lin_wacu
# Anneke Sicherer-Roetman MSG MARIN 20071105

NVCC    = nvcc
CC      = gcc
CPP     = g++
CONVERT = gtk-builder-convert
GTEST_DIR=~/gtest-1.7.0
OSG_DIR = ~/Downloads/OpenSceneGraph-3.2.1-rc

#CFLAGS  = -I include -I /usr/local/include -I /opt/cuda/include #-DSMOOTHING

CFLAGS  = -I include -I /usr/local/include  -I /usr/local/cuda/include -I ${GTEST_DIR}/include/ -I ${OSG_DIR}/include   #-DSMOOTHING

NVCCFLAGS := ${CFLAGS} -arch=sm_21 -Xcompiler -Wall # -keep  

CFLAGS += -Wall
#LFLAGS  = -L /usr/local/lib -L /opt/cuda/lib64 -L /opt/cuda/lib -lcublas -lcudart -lm

LFLAGS  = -L /usr/local/cuda/lib64  -L /usr/local/lib -L /usr/local/lib64 -L /usr/local/cuda/lib -L ${OSG_DIR}/lib  -lcublas -lcudart -lm 

LFLAGS+=-lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lglib-2.0  -losg -losgViewer -losgGA -losgText -losgDB

# testprogram for interactive waves
PROGRAM1 = lin_wacu test

# designprogram for path of pressure pulse
PROGRAM2 = preppath

# unittest
TEST     = test
COVERAGE = coverage.svg
UNITTEST = unittests
CTFLAGS  = ${CFLAGS} -DUNITTEST -fprofile-arcs -ftest-coverage
LTFLAGS  = -fprofile-arcs -ftest-coverage -L ${GTEST_DIR}/GTEST_Build/ -lgcov -lgtest -lgtest_main -lpthread

OBJS1    = \
	lin_wacu.o \
	InteractiveWaves.o \
	WavesComputer.o \
	LowPassFilter.o \
	WeakRefl.o \
	IncomingWaves.o \
	FileUtils.o \
	FloatUtils.o \
	StringUtils.o \
	FileReader.o \
	Interpolator.o \
	PulseManager.o \
	PulseControl.o \
	GeometryManager.o \
	Geometry.o \
	MathGeometry.o \
	ShipGeometry.o \
	ShipForces.o \
	ShipPulse.o \
	MathPulse.o \
	Pulse.o \
	PulseFactory.o \
	Panel.o \
	Panels.o \
	Timer.o \
	src/mml/primitives.o \
	src/mml/reducepolygon.o \
	src/mml/analysepolygon.o \
	src/mml/flattenPolygon.o \
	RrbSolver.o \
	Cg4Solver.o \
	matvec.o \
	RrbAkashSolver.o \
	Solver.o \
	SolverFactory.o \
	CudaRrbSolver.o \
	CudaWavesComputer.o \
	CudaIncomingWaves.o 	

OBJS2    = \
	preppath.o \
	FileReader.o \
	FileUtils.o \
	StringUtils.o \
	Interpolator.o \
	Timer.o \
	Plotter.o \
	OsgPlotter.o \
	OsgViewer.o \
	PrepWindow.o \
	GtkAppWindow.o

TESTOBJS =  \
	Array1DTestX.o \
	FloatUtilsX.o \
	FloatUtilsTestX.o \
	Array2DTestX.o

ifeq ($(allsolvers),1)
CFLAGS += -DSOLVERS
OBJS1 +=  \
	Cg4Solver.o \
	Cg4ElwinSolver.o \
	CholSolver.o \
	MicDefSolver.o \
	NopSolver.o \
	NopDefSolver.o \
	NopElwinSolver.o \
	RicSolver.o \
	RicDefSolver.o \
	RicDefEisSolver.o \
	RrbDefSolver.o \
	RrbElwinSolver.o \
	DefSolver.o
endif

ifeq ($(double),1)
	CFLAGS += -DUSEDOUBLE
endif

ifeq ($(cuda),1)
	CFLAGS += -DUSE_CUDAWAVESCOMPUTER
endif

ifeq ($(cuda),2)
	CFLAGS += -DUSE_CUDAINCOMINGWAVES
endif

ifeq ($(cuda),3)
	CFLAGS += -DUSE_CUDAWAVESCOMPUTER -DUSE_CUDAINCOMINGWAVES
endif

ifeq ($(max),1)
	CFLAGS += -DMAXWAVES
endif

ifeq ($(indices),1)
	CFLAGS += -Dindices
endif

ifeq ($(spy),1)
	CFLAGS += -Ddelft
	LFLAGS += -lpngwriter -lfreetype -lpng
endif

ifeq ($(delft),1)
	CFLAGS += -Ddelft
endif

ifeq ($(test),1)
	CFLAGS += -DTEST
	#LFLAGS += -lprofiler
endif

ifeq ($(parallel),1)
	CFLAGS += -fopenmp -Dopenmp1
	NVCCFLAGS += -Xcompiler -fopenmp
	LFLAGS += -lgomp
endif

ifeq ($(parallel),2)
	CFLAGS += -fopenmp -Dopenmp2
	NVCCFLAGS += -Xcompiler -fopenmp
	LFLAGS += -lgomp
endif

ifeq ($(parallel),3)
	CFLAGS += -fopenmp -Dopenmp3
	NVCCFLAGS += -Xcompiler -fopenmp
	LFLAGS += -lgomp
endif

ifeq ($(parallel),4)
	CFLAGS += -fopenmp -Dopenmp1 -Dopenmp3
	NVCCFLAGS += -Xcompiler -fopenmp
	LFLAGS += -lgomp
endif

ifeq ($(plot),1)
	CFLAGS += -DWITH_PLOT 
        CFLAGS += `pkg-config --cflags gtk+-2.0`
        LFLAGS += `pkg-config --libs gtk+-2.0` -export-dynamic
        LFLAGS += -lX11
        LFLAGS += -losg -losgViewer
        OBJS1 += ControlWindow.o GtkAppWindow.o Plotter.o OsgPlotter.o PlotterFactory.o OsgViewer.o
endif

ifeq ($(png),1)
	CFLAGS += -DNO_FREETYPE
	LFLAGS += -lpngwriter
endif

ifeq ($(debug), 1)
	CFLAGS += -g -DDEBUG
	NVCCFLAGS += -g -G -Xcompiler -g
else
	CFLAGS += -O3
endif

ifeq ($(debug), 1)
all: ${PROGRAM1} ${PROGRAM2} ${TEST}
else
all: ${PROGRAM1} ${PROGRAM2}
endif

${PROGRAM1}: ${OBJS1} ${UNITTEST} controlWindow.xml
	${CPP} -o $@ ${OBJS1} ${LFLAGS} 
	
${PROGRAM2}: ${OBJS2} ${UNITTEST} prepWindow.xml
	${CPP} -o $@ ${OBJS2} ${LFLAGS} 

${TEST}: ${COVERAGE}

${COVERAGE}: ${UNITTEST}
#	trucov

${UNITTEST}: ${TESTOBJS}
	${CPP} -o $@ ${TESTOBJS} ${LTFLAGS}
	@echo running tests
	./${UNITTEST}
	
%.o : %.cpp
	${CPP} ${CFLAGS} -o $@ -c $<

%X.o : %.cpp
	${CPP} ${CTFLAGS} -o $@ -c $<

%.o : %.c
	${CC} ${CFLAGS} -o $@ -c $<

%.o : %.cu
	${NVCC} ${NVCCFLAGS} -o $@ -c $<
	
%.xml : %.glade
	${CONVERT} $< $@ 

clean:
	rm -f *.o *.mod core *~ *.bak *.xml *.gladep *.gcno *.gcda ${TEST}

